<?php
include_once substr(__FILE__, 0, strrpos(__FILE__, '/')).'/SocialNetwork.php';

define('APIURL','https://api.linkedin.com');
define('REQUESTTOKEN','/uas/oauth/requestToken');
define('AUTHORIZEPATH','/uas/oauth/authorize?oauth_token=');
define('ACCESSTOKENPATH','/uas/oauth/accessToken');

class LinkedIn
    extends SocialNetwork
{
    public function  __construct($applicationName, $apiKey, $secretKey, $callback = null) {
        parent::__construct($applicationName, $apiKey, $secretKey, $callback);
        $this->networkName = 'LinkedIn';
        $this->apiUrl = APIURL;
        $this->requestTokenPath = REQUESTTOKEN;
        $this->authorizePath = AUTHORIZEPATH;
        $this->accessTokenPath = ACCESSTOKENPATH;
    }

    /**
     * Get link to requestToken
     * @param $username the username of the LinkedIn user. 
     * @return result of $this->fetch
     */
    public function requestToken($username)
    {
        $link = $this->requestTokenUrl;
        $link.= '&oauth_callback='.urlencode($this->callback);
    }

    /**
     * Save the status update to linkedin.
     * @param $text string to be posted as the status
     * @return result of $this->fetch
     */
    public function statusUpdate($text) {

            $this->authorize();

            $parameters = '<?xml version="1.0" encoding="UTF-8"?><current-status>'.$text.'</current-status>';

            try {
                    $this->oauth->fetch($this->apiUrl.'/v1/people/~/current-status',$parameters,OAUTH_HTTP_METHOD_PUT);
                    return true;
            } catch(Exception $e) {
                    echo '<pre>Error: PUT current-status. Please try again.</pre>';
                    return false;
            }

    }

    /**
     * TODO: Test
     */
    public function getNetworkUpdates()
    {
        $this->authorize();

        $parameters = 	'<activity locale="en_US">'
                                        .'<timestamp>'.time().'</timestamp>'
                                        .'<content-type>linkedin-html</content-type>'
                                        .'</activity>';

        $headers['Content-Type'] = 'text/xml;charset=UTF-8';

        try {
                $this->oauth->fetch($this->apiUrl.'/v1/people/~/network?type=JGRP',null,OAUTH_HTTP_METHOD_GET,$headers);
                echo '<pre>';
                print_r($this->oauth->getLastResponse());
                print_r($this->oauth->getLastResponseInfo());
                print_r($this->oauth);
                echo '</pre>';
                return true;
        } catch(Exception $e) {
                echo '<pre>Error: POST person-activities. Please try again.</pre>';
                return false;
        }
    }

    /**
     * TODO: Test
     */
    public function getConnections()
    {
        $this->authorize();

        $parameters = 	'<activity locale="en_US">'
                                        .'<timestamp>'.time().'</timestamp>'
                                        .'<content-type>linkedin-html</content-type>'
                                        .'</activity>';

        $headers['Content-Type'] = 'text/xml;charset=UTF-8';

        $this->oauth->enableDebug();

        try {
                $this->oauth->fetch($this->apiUrl.'/v1/people/~/connections',null,OAUTH_HTTP_METHOD_GET,$headers);
                echo '<pre>';
                print_r($this->oauth->getLastResponse());
                print_r($this->oauth->getLastResponseInfo());
                print_r($this->oauth);
                echo '</pre>';
                return true;
        } catch(Exception $e) {
                echo '<pre>Error: POST person-activities. Please try again.</pre>';
                return false;
        }
    }


    /**
     * TODO: Test
     */
    public function getGroup($id)
    {
        $this->authorize();

        $parameters = 	'<activity locale="en_US">'
                                        .'<timestamp>'.time().'</timestamp>'
                                        .'<content-type>linkedin-html</content-type>'
                                        .'</activity>';

        $headers['Content-Type'] = 'text/xml;charset=UTF-8';

        $this->oauth->enableDebug();

        try {
                $this->oauth->fetch($this->apiUrl.'/v1/group/id=2412126',null,OAUTH_HTTP_METHOD_GET,$headers);
                echo '<pre>';
                print_r($this->oauth->getLastResponse());
                print_r($this->oauth->getLastResponseInfo());
                print_r($this->oauth);
                echo '</pre>';
                return true;
        } catch(Exception $e) {
            echo $e->getMessage();
                echo '<pre>Error: POST person-activities. Please try again.</pre>';
                return false;
        }
    }

    /**
     * Fetch data of a user.
     * @param $options array of options:
     * 'data' empty for users profile, 'connections' for users connections, 'network' for users network updates
     * 'profile' string default: '~'. info: http://developer.linkedin.com/docs/DOC-1002
     * 'fields' array of fields. info: http://developer.linkedin.com/docs/DOC-1061
     * @return result of $this->fetch
     */
    public function getData($options = array('data'=>'','profile'=>'~','fields'=>array())) {

            if(empty($options['profile'])) $options['profile'] = '~';

            $query = '/v1/people/'.$options['profile'];

            if(!empty($options['data'])) {
                    $query .= '/'.$options['data'];
            }

            if(is_string($options['fields']) && $options['fields'] == 'all') {
                    $query .= ':full';
            } elseif(!empty($options['fields'])) {
                    $query .= ':('.implode(',',$options['fields']).')';
            }
            //echo $this->apiUrl.$query;

            return $this->get($this->apiUrl.$query);
    }
}
?>
